package com.sunshine.domain.service.impl;

import com.sunshine.domain.convert.SubjectInfoConvert;
import com.sunshine.domain.entity.SubjectInfoBO;
import com.sunshine.domain.handler.factory.SubjectTypeHandlerFactory;
import com.sunshine.domain.handler.subject.subjectTypeHandler;
import com.sunshine.domain.service.SubjectInfoDomainService;
import com.sunshine.infra.basic.entity.SubjectInfo;
import com.sunshine.infra.basic.entity.SubjectMapping;
import com.sunshine.infra.basic.service.SubjectInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 题目信息表(SubjectInfo)业务实现类
 * @version 1.0
 * @Author 夏百友
 * @Date 2025/11/7 18:31
 */
@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {


    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;

    @Resource
    private SubjectInfoService subjectInfoService;


    @Override
    public void addSubjectInfo(SubjectInfoBO subjectInfoBO) {
        if (log.isInfoEnabled()){
            log.info("domain.SubjectInfoDomainService.addSubjectInfo:参数{}",subjectInfoBO);
        }
        SubjectInfo subjectInfo = SubjectInfoConvert.INSTANCE.convertBoToInfo(subjectInfoBO);
        subjectInfoService.insert(subjectInfo);
        //
        subjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfoBO.getSubjectType());
        handler.add(subjectInfoBO);
        List<Integer> categroryIds = subjectInfoBO.getCategroryIds();
        List<Integer> labelIds = subjectInfoBO.getLabelIds();
        List<SubjectMapping> subjectMappingList = new LinkedList<>();
        categroryIds.forEach(item -> {
            labelIds.forEach(item1 -> {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setSubjectId(subjectInfo.getId().longValue());
                subjectMapping.setCategoryId(item.longValue());
                subjectMapping.setLabelId(item1.longValue());
                // 修复：将创建的subjectMapping对象添加到列表中
                subjectMappingList.add(subjectMapping);

            });
        });
        subjectInfoService.batchInsert(subjectMappingList);
    }
}